package jp.co.hcs.attendancemanagement.common;

import java.sql.SQLException;

import jp.co.hcs.attendancemanagement.data.dao.ApprovalCompanyDAO;
import jp.co.hcs.attendancemanagement.data.dao.ApprovalDepartmentDAO;
import jp.co.hcs.attendancemanagement.data.dao.ApprovalDivisionDAO;
import jp.co.hcs.attendancemanagement.data.dao.ApprovalEmployeeDAO;
import jp.co.hcs.attendancemanagement.data.dao.EmployeeMasterDAO;
import jp.co.hcs.attendancemanagement.data.entity.ApprovalCompany;
import jp.co.hcs.attendancemanagement.data.entity.ApprovalDepartment;
import jp.co.hcs.attendancemanagement.data.entity.ApprovalDivision;
import jp.co.hcs.attendancemanagement.data.entity.ApprovalEmployee;
import jp.co.hcs.attendancemanagement.data.entity.EmployeeMaster;
import jp.co.hcs.attendancemanagement.logic.LogicBase;

/**
 * The Class ApprovalGroupGetting.
 */
public class ApprovalGroupGetting extends LogicBase {


  /**
   * Retrieve approval group id.
   * 
   * @param employeeId the employee id
   * @param approvalTypeId the approval type id
   * @return the approval group id
   * @throws SQLException the sQL exception
   */
  public String retrieveApprovalGroupId(String employeeId, String approvalTypeId)
      throws SQLException {
    EmployeeMasterDAO employeeDao = daoFactory.getEmployeeMasterDAO();

    // get organization information
    EmployeeMaster currentEmployee = employeeDao.selectEmployeeMasterByPrimaryKey(employeeId);
    String companyCode = currentEmployee.getCompanyId();
    String divisionCode = currentEmployee.getDivisionId();
    String departmentCode = currentEmployee.getDepartmentId();

    // find approval group id in ApprovalEmployee (TB5_SNWARIATE_SHAIN)
    ApprovalEmployeeDAO appEmpDao = daoFactory.getApprovalEmployeeDAO();
    try {
      ApprovalEmployee appEmpObj =
          appEmpDao.selectApprovalEmployeeByPrimaryKey(employeeId, approvalTypeId);
      if (appEmpObj != null) {
        return appEmpObj.getApprovalGroupId();
      }
    } catch (SQLException e) {
      // do nothing
      // will find approval group id in next table
    }

    // find approval group id in ApprovalDepartment (TB5_SNWARIATE_KA)
    ApprovalDepartmentDAO appDeptDao = daoFactory.getApprovalDepartmentDAO();
    try {
      ApprovalDepartment appDeptObj =
          appDeptDao.selectApprovalDepartmentByPrimaryKey(divisionCode, companyCode,
              departmentCode, approvalTypeId);
      if (appDeptObj != null) {
        return appDeptObj.getApprovalGroupId();
      }
    } catch (SQLException e) {
      // do nothing
      // will find approval group id in next table
    }

    // find approval group id in ApprovalDivision (TB5_SNWARIATE_BU)
    ApprovalDivisionDAO appDivDao = daoFactory.getApprovalDivisionDAO();
    try {
      ApprovalDivision appDivObj =
          appDivDao.selectApprovalDivisionByPrimaryKey(divisionCode, companyCode, approvalTypeId);
      if (appDivObj != null) {
        return appDivObj.getApprovalGroupId();
      }
    } catch (SQLException e) {
      // do nothing
      // will find approval group id in next table
    }

    // find approval group id in ApprovalCompany (TB5_SNWARIATE_KAISHA)
    ApprovalCompanyDAO appCoDao = daoFactory.getApprovalCompanyDAO();
    try {
      ApprovalCompany appCoObj =
          appCoDao.selectApprovalCompanyByPrimaryKey(companyCode, approvalTypeId);
      if (appCoObj != null) {
        return appCoObj.getApprovalGroupId();
      }
    } catch (SQLException e) {
      // do nothing
    }

    return null;
  }
}
